// pages/more/more.js
const util = require("../../../utils/util.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hiddenLoading: true,
    filterId: 1,
    tablist:0,
    tabName:"全部类型",
    chioceAll: false,
    chioceDiscount: false,
    chioceRecently:false, 
    hint: true, 
    hints: true,   
    keyword:"",
    page:1,
    limit:10,
    pagecount:0,
    discount:0, //值 等于1 是按照折扣排序
    distance: 0, //值 等于1 是按照距离排序
    slecteItem:[
      {
        "slecteIcon": "/image/more_24.png",
        "slecteIcons": "/image/more_23.png",
        "slecteText": "全部类型",
        "active": "/image/more_11.png"
      },{
      "slecteIcon": "/image/more_13.png",
      "slecteIcons": "/image/more_14.png",
      "slecteText":"餐饮美食",
      "active":"/image/more_11.png"
    }, {
      "slecteIcon": "/image/more_18.png",
      "slecteIcons": "/image/more_17.png",
      "slecteText": "酒店娱乐",
      "active": "/image/more_11.png"
    }, {
      "slecteIcon": "/image/more_20.png",
      "slecteIcons": "/image/more_19.png",
      "slecteText": "生活服务",
      "active": "/image/more_11.png"
    }, {
      "slecteIcon": "/image/more_22.png",
      "slecteIcons": "/image/more_21.png",
      "slecteText": "超市百货",
      "active": "/image/more_11.png"
    }]
  },
  tapFilter: function (e) {
    var that = this;     
    this.setData({
      filterId: e.target.dataset.id
    });
    switch (e.currentTarget.dataset.id) {
      case "1":
        if (this.data.chioceAll) {
          this.setData({
            chioceAll: false,
            chioceDiscount: false,
            chioceRecently: false,
          });
        }
        else {
          this.setData({
            chioceAll: true,
            chioceDiscount: false,
            chioceRecently: false,

          });
        }
        break;
      case "2":
        if (this.data.chioceAll) {
          this.setData({
            chioceAll: false,
            chioceDiscount: false,
            chioceRecently: false,
          });
        }
        else {
          this.setData({
            chioceAll: false,
            chioceDiscount: false,
            chioceRecently: false,

          });
        }
        break;
      case "3":
        if (this.data.chioceAll) {
          this.setData({
            chioceAll: false,
            chioceDiscount: false,
            chioceRecently: false,
          });
        }
        else {
          this.setData({
            chioceAll: false,
            chioceDiscount: false,
            chioceRecently: false,

          });
        }
        break;  
    }
    var typeid = that.data.tablist;//当前分类ID
    var tabid = e.target.dataset.id;   
    if(tabid==2){
      that.setData({ discount: 1, distance: 0, page: 1})  //筛选条件
      that.GetSupplierList(typeid, function (data) {
        that.setData({ SupplierList: data })
      });
    }else if(tabid==3){
      that.setData({ discount: 0, distance: 1 ,page:1})  //筛选条件
      that.GetSupplierList(typeid, function (data) {
        that.setData({ SupplierList: data })
      });
    }
  },
  tabList:function(e){
    var that = this;
    this.setData({
      tablist: e.currentTarget.dataset.index, 
      tabName: e.currentTarget.dataset.name,
      chioceAll: false,
    });
    util.ShowLoading("查询中...");
    var typeid = e.currentTarget.dataset.index;//当前分类ID
    that.setData({ discount: 0, distance: 0 ,page:1})  //筛选条件
    that.GetSupplierList(typeid, function (data) {
      that.setData({ SupplierList: data })
    });
  },
  hideAllChioce:function(e){
    this.setData({
      chioceAll: false,
      chioceDiscount: false,
      chioceRecently: false,
    })
  },
  //详情
  goDetail: function (e) {    
    var sid = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../detail/detail?sid='+sid
    })
  },
  showMask(e) {
    //显示遮罩
    this.setData({
      hasMask: true
    })
  },
  hideMask(e) {
    //隐藏遮罩
    this.setData({
      hasMask: false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {    
    var that = this;
    that.ShowLoading();
    if (options.typeid != undefined)
    {
      that.setData({
        tablist: options.typeid,
        tabName: options.title,
        chioceAll: false,
      });
    }
    if (options.keyword != undefined){
      that.setData({ keyword: options.keyword});
    }
    
    that.GetSupplierList(options.typeid, function (list) {//根据类型加载
      that.setData({ SupplierList: list });
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    var page =  that.data.page+1;
    var pagecount = that.data.pagecount/10;    
    if (parseInt(pagecount)!=pagecount){  //获取总页数
      pagecount = parseInt(pagecount)+1;
    }
    if(page<=pagecount){
      that.ShowLoading();
      that.setData({ page: page });
      var typeid = that.data.tablist; //当前分类ID
      that.setData({ discount: 0, distance:0})  //筛选条件
      that.GetSupplierList(typeid,function(data){
        that.setData({ SupplierList: that.data.SupplierList.concat(data), hints: true})
      });
    }else{
      console.log("已经加载所有了");
      that.setData({ hints: false });

      wx.stopPullDownRefresh(); //停止下拉刷新
    }  
  },
 
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  ShowLoading: function () {
    this.setData({
      hiddenLoading: false
    });
    var that = this;
    setTimeout(function () {
      wx.stopPullDownRefresh();
      that.setData({
        hiddenLoading: true
      });
    }, 1500);
  },

  GetSupplierList: function (typeid,callback){ //根据商品类型、折扣、距离、关键词获取商户信息
    var that = this;
    var discount = that.data.discount;  //折扣
    var distance = that.data.distance;  //距离
    var page = that.data.page;  //当前页码
    var limit = that.data.limit;  //每页数据个数
    var keyword = that.data.keyword;  //搜索词
    wx.getStorage({
      key: 'AddressInfo',
      success: function (res) {  //先把存储的经纬度获取到
        var data = {
          lat: res.data.location.lat,
          lng: res.data.location.lng,
          page: page,
          limit: limit,
          typeid:typeid,
          discount: discount,
          distance: distance,
          keyword: keyword
        };
        data.act = "Find_SupplierByTypeAndDiscountAndAddress";
        util.Ajax(data, function (json) {
          that.setData({ pagecount:json.count})
          if (json.data.length > 0) {
            callback(json.data);    
            that.setData({  hint: true });    
          }else{
            that.setData({ SupplierList: [],hint:false});
            console.log("没有查询到信息");
          }

        });
      }
    })
  }
})